<?php

class LayoutContainerService extends FServiceBase
{    
    /**
    * find layouts by container
    * 
    * @param array() $params
    *   array(
    *   'container'=>'container value',
    * )
    */
    public function findLayoutsByContainer($params)
    {
        $container = $this->getParam($params, 'container', '');
        $pageId = $this->getParam($params, 'page_id', 0);
        $revisionId = $this->getParam($params, 'revision_id', 0);
        
        //find layout by container
        $criteria = new CDbCriteria;
        $criteria->with = array('layout');
        $criteria->compare('container', $container);
        $criteria->compare('page_id', $pageId);
        $criteria->compare('revision_id', $revisionId);
        $model = LayoutContainer::model()->find($criteria);
        if (is_object($model) && is_object($model->layout))
        {
            $data = unserialize($model->layout->data);
            if (is_array($data) && count($data))
            {
                $containers = array();
                $html = CHtml::openTag('div', array('class'=>$container.'_wrapper'));
                foreach ($data as $rowIndex => $rows)
                {
                    $rowClass = $container.'_row_'.$rowIndex;
                    $html .= CHtml::openTag('div', array('class'=>$rowClass));
                    foreach ($rows as $columnIndex => $columnWidth)
                    {
                        $columnClass = $rowClass.'_col_'.$columnIndex;
                        if ($rowIndex == 0 && $columnIndex == 0)
                        {
                            //render origin container
                            $id = $container;
//                            $columnClass .= ' disable-change-layout';
                        }
                        else
                        {
                            $id = $columnClass;
                            array_push($containers, $id);
                        }
                        $columnClass .= ' sb-container';
                        $html .= CHtml::tag('div', array('style'=>'width:'.$columnWidth.'px; float: left;','class'=>$columnClass,'id'=>$id));
                    }
                    $html .= CHtml::tag('div', array('style'=>'clear:both;'));
                    $html .= CHtml::closeTag('div');
                }
                $html .= CHtml::closeTag('div');
                $this->result->processed('containers', $containers);
                return $this->result->processed('html', $html);
            }
        }
        return $this->result->processed('html', false);
    }
    
    public function update($params)
    {
        $container = $this->getParam($params, 'container', '');
        $pageId = $this->getParam($params, 'page_id', '');
        $revisionId = $this->getParam($params, 'revision_id', '');
        $layoutId = $this->getParam($params, 'layout_id', '');
        
        $criteria = new CDbCriteria;
        $criteria->compare('container', $container);
        $criteria->compare('page_id', $pageId);
        $criteria->compare('revision_id', $revisionId);
        $model = LayoutContainer::model()->find($criteria);
        if (!is_object($model))
        {
            $model = new LayoutContainer;
            $model->container = $container;
            $model->page_id = $pageId;
            $model->revision_id = $revisionId;
        }
        $model->layout_id = $layoutId;
        if (!$model->save())
        {
            Yii::log(CVarDumper::dumpAsString($model->getErrors()), CLogger::LEVEL_ERROR);
        }
        return $this->result;
    }
    
    public function find($params)
    {
        $container = $this->getParam($params, 'container', '');
        $pageId = $this->getParam($params, 'page_id', '');
        $revisionId = $this->getParam($params, 'revision_id', '');
        $layoutId = $this->getParam($params, 'layout_id', '');
        $criteria = new CDbCriteria;
        $criteria->compare('container', $container);
        $criteria->compare('page_id', $pageId);
        $criteria->compare('revision_id', $revisionId);
        $criteria->compare('layout_id', $layoutId);
        $model = LayoutContainer::model()->find($criteria);
        return $this->result->processed('model', $model);
    }
    
    public function remove($params)
    {
        $container = $this->getParam($params, 'container', '');
        $pageId = $this->getParam($params, 'page_id', '');
        $revisionId = $this->getParam($params, 'revision_id', '');
        $criteria = new CDbCriteria;
        $criteria->compare('container', $container);
        $criteria->compare('page_id', $pageId);
        $criteria->compare('revision_id', $revisionId);
        LayoutContainer::model()->deleteAll($criteria);
    }
}